package com.allenyuan.gui;
import java.awt.CardLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JTextField;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;

public class GCodeImportPanel extends AbstractPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = -2231038504662200836L;
	private JTextField filePathText;

	/**
	 * Create the panel.
	 */
	public GCodeImportPanel(GuiContext context) {
		super(context);
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{0, 51, 0};
		gridBagLayout.rowHeights = new int[]{179, 0};
		gridBagLayout.columnWeights = new double[]{1.0, 0.0, Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[]{0.0, Double.MIN_VALUE};
		optionPanel.setLayout(gridBagLayout);
		
		filePathText = new JTextField();
		GridBagConstraints gbc_filePathText = new GridBagConstraints();
		gbc_filePathText.fill = GridBagConstraints.HORIZONTAL;
		gbc_filePathText.insets = new Insets(0, 0, 0, 5);
		gbc_filePathText.gridx = 0;
		gbc_filePathText.gridy = 0;
		optionPanel.add(filePathText, gbc_filePathText);
		filePathText.setColumns(10);
		
		JButton fileOpenButton = new JButton("...");
		fileOpenButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				JFileChooser chooser = new JFileChooser();
				int ret = chooser.showOpenDialog(GCodeImportPanel.this);
				if (ret == JFileChooser.APPROVE_OPTION) {
					filePathText.setText(chooser.getSelectedFile().getAbsolutePath());
					context.setFile(chooser.getSelectedFile());
				}
			}
		});
		GridBagConstraints gbc_fileOpenButton = new GridBagConstraints();
		gbc_fileOpenButton.anchor = GridBagConstraints.WEST;
		gbc_fileOpenButton.gridx = 1;
		gbc_fileOpenButton.gridy = 0;
		optionPanel.add(fileOpenButton, gbc_fileOpenButton);
	}

	@Override
	protected String getGuideText() {
		// TODO Auto-generated method stub
		StringBuilder sb = new StringBuilder();
		sb.append("请导入需雕刻使用的GCode文件(*.txt)，或者导入矢量图文件(*.stl)，导入后将会转换成GCode文件.\n");
		sb.append("点击编辑按钮可以进行编译及预览.\n");
		return sb.toString();
	}

	@Override
	protected String getGuideTitle() {
		// TODO Auto-generated method stub
		return "导入GCode文件";
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if (e.getActionCommand().equals(lastButton.getActionCommand())) {
			((CardLayout)getParent().getLayout()).show(getParent(), ModeSelectPanel.class.getName());
		}
		else if (e.getActionCommand().equals(nextButton.getActionCommand())) {
			((CardLayout)getParent().getLayout()).show(getParent(), PrintProgressPanel.class.getName());
		}
	}

}
